<?php
namespace app\controller;

use app\controller\Response;
use app\BaseController;

class Index extends BaseController
{
    public function index()
    {
        //echo "hello world";
        $publicKey  = file_get_contents('./cert/#@!$pubkey.pem');
        $privateKey = file_get_contents('./cert/#@!$privkey.pem');
        // create storage
        $storage = new \OAuth2\Storage\Memory(array(
            'keys' => array(
                'public_key'  => $publicKey,
                'private_key' => $privateKey,
            ),
            // add a Client ID for testing
            'client_credentials' => array(
                'CLIENT_ID' => array('client_secret' => 'CLIENT_SECRET')
            ),
        ));
        $server = new \OAuth2\Server($storage, array(
            'use_jwt_access_tokens' => true,
        ));

        $server->addGrantType(new \OAuth2\GrantType\ClientCredentials($storage)); // minimum config

        $object =  $server->handleTokenRequest(\OAuth2\Request::createFromGlobals());
        $res = Response::succJson($object->parameters,0,'获取token成功!');
        return json($res);

    }
    public function index1(){
        /* for a Resource Server (minimum config) */
        $publicKey  = file_get_contents('./cert/#@!$pubkey.pem');

        // no private key necessary
        $keyStorage = new \OAuth2\Storage\Memory(array('keys' => array(
            'public_key'  => $publicKey,
        )));

        $server = new \OAuth2\Server($keyStorage, array(
            'use_jwt_access_tokens' => true,
        ));

        // verify the JWT Access Token in the request
        if (!$server->verifyResourceRequest(\OAuth2\Request::createFromGlobals())) {
            exit("Failed");
        }
        echo "Success!";

    }
}
